package com.attunlcok.unlock.constant;

/**
 * 解锁状态定义类
 * 统一管理系统中所有解锁任务状态
 */
public class UnlockStatus {
    // 内部状态定义 - 使用String与数据库一致
    public static final String PENDING = "0";       // 待处理
    public static final String SUCCESS = "1";       // 成功
    public static final String FAILED = "2";        // 失败
    public static final String CONFIRMED = "3";     // 已确认邮件
    public static final String TECH_FAILED = "4";   // 技术失败
    public static final String PROCESSING = "5";    // 处理中
    public static final String DENIED = "7";        // 解锁被拒绝
    public static final String UNKNOWN = "8";       // 未知状态
    public static final String APPROVED = "9";      // 解锁批准
    
    // DHRU API状态定义 - int类型
    public static final int DHRU_SUCCESS = 1;      // 成功
    public static final int DHRU_FAILED = 2;       // 失败
    public static final int DHRU_PROCESSING = 3;   // 处理中
    public static final int DHRU_WAITING = 4;      // 等待中
    public static final int DHRU_CANCELED = 5;     // 已取消
    
    /**
     * 获取状态描述
     */
    public static String getStatusDesc(String status) {
        switch (status) {
            case PENDING: return "待处理";
            case SUCCESS: return "解锁成功";
            case FAILED: return "解锁失败";
            case CONFIRMED: return "已确认邮件";
            case TECH_FAILED: return "技术失败";
            case PROCESSING: return "处理中";
            case DENIED: return "解锁被拒绝";
            case UNKNOWN: return "未知状态";
            case APPROVED: return "解锁批准";
            default: return "未知状态";
        }
    }
    
    /**
     * 将内部状态转换为DHRU API状态
     */
    public static int toDhruStatus(String internalStatus) {
        switch (internalStatus) {
            case SUCCESS:
            case APPROVED:
                return DHRU_SUCCESS;
            case FAILED:
            case DENIED:
                return DHRU_FAILED;
            case TECH_FAILED:
                return DHRU_PROCESSING; // 技术失败映射为处理中，允许重试
            case PROCESSING:
                return DHRU_PROCESSING;
            case PENDING:
                return DHRU_WAITING;
            case CONFIRMED:
                return DHRU_PROCESSING; // 已确认邮件状态映射为处理中
            case UNKNOWN:
            default:
                return DHRU_PROCESSING; // 未知状态默认为处理中
        }
    }
    
    /**
     * 从Python状态转换为内部状态
     */
    public static String fromPythonStatus(int pythonStatus) {
        switch (pythonStatus) {
            case 1: return SUCCESS;      // STATUS_SUCCESS
            case 2: return FAILED;       // STATUS_FAILED
            case 3: return CONFIRMED;    // STATUS_CONFIRMED
            case 4: return TECH_FAILED;  // STATUS_TECH_FAILED
            case 5: return PENDING;      // STATUS_PENDING
            case 6: return PROCESSING;   // STATUS_PROCESSING
            case 7: return DENIED;       // STATUS_DENIED
            case 8: return UNKNOWN;      // STATUS_UNKNOWN
            case 9: return APPROVED;     // STATUS_APPROVED
            default: return UNKNOWN;
        }
    }
    
    /**
     * 判断是否为终态
     */
    public static boolean isFinalStatus(String status) {
        return SUCCESS.equals(status) || 
               FAILED.equals(status) || 
               APPROVED.equals(status) || 
               DENIED.equals(status);
    }
    
    /**
     * 判断是否需要继续查询状态
     */
    public static boolean needStatusQuery(String status) {
        return CONFIRMED.equals(status) && !isFinalStatus(status);
    }
    
    /**
     * 判断是否可以重试
     */
    public static boolean canRetry(String status) {
        return TECH_FAILED.equals(status);
    }
} 